package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.jdbc.utils.ClassTools;
import com.gzhryc.jdbc.utils.JsonTools;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.db.PayAccountGroup;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayConfig;
import com.gzhryc.shared_device.common.payment.dao.models.CashWithdrawalConfig;
import com.gzhryc.shared_device.common.payment.services.PayAccountGroupService;
import com.gzhryc.shared_device.common.payment.services.WXPayConfigService;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.config.PayAccountGroupInfoRequest;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.config.PayAccountGroupInfoResponse;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.config.PayAccountGroupListResponse;

public class PayAccountGroupController {

	@Route(value = "/agent/api/v1/config/pay_account/list.data")
	public PayAccountGroupListResponse listData(HttpServletRequest request) {
		PayAccountGroupListResponse apiResp = new PayAccountGroupListResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
		LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
		if (loginUser != null) {
			List<PayAccountGroup> payAccountGroupList = PayAccountGroupService.self().findByUserId(loginUser.getId());
			if (payAccountGroupList != null && payAccountGroupList.size() > 0) {
				apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
				apiResp.getBody().setItems(new ArrayList<>());
				for (PayAccountGroup payAccountGroup : payAccountGroupList) {
					PayAccountGroupListResponse.Item item = new PayAccountGroupListResponse.Item();
					item.setId(payAccountGroup.getId());
					item.setName(payAccountGroup.getName());
					item.setCanTransfer(payAccountGroup.getCanTransfer());
					item.setMemberCanTransfer(payAccountGroup.getMemberCanTransfer());
					item.setCreateDate(payAccountGroup.getCreateDate());
					apiResp.getBody().getItems().add(item);
				}
			}
		}
		return apiResp;
	}

	@Route(value = "/agent/api/v1/config/pay_account/info.data")
	public PayAccountGroupInfoResponse walletInfo(HttpServletRequest request) {
		PayAccountGroupInfoResponse apiResp = new PayAccountGroupInfoResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
		PayAccountGroupInfoRequest apiReq = WebUtils.getJsonRequest(request, PayAccountGroupInfoRequest.class);
		LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
		if (apiReq != null && apiReq.getBody().getId() != null && loginUser != null) {
			PayAccountGroup payAccountGroup = PayAccountGroupService.self().getById(apiReq.getBody().getId());
			if (payAccountGroup != null && payAccountGroup.getUserId().equals(loginUser.getId())){
				apiResp.setCode(ClientAPICodeConstants.SUCCESS, null);
				ClassTools.copy(apiResp.getBody(), payAccountGroup);
				apiResp.getBody().setWxTransferAppTypeName(payAccountGroup.getWxTransferAppTypeName());
				apiResp.getBody().setDayTotalMoneyStr(payAccountGroup.getDayTotalMoneyStr());
				apiResp.getBody().setMemberDayTotalMoneyStr(payAccountGroup.getMemberDayTotalMoneyStr());
				if(StringTools.isNotBlank(payAccountGroup.getWxTransferAppId())) {
					WXPayConfig wxPayConfig = WXPayConfigService.self().getByAppId(payAccountGroup.getWxTransferAppId());
					if(wxPayConfig != null) {
						apiResp.getBody().setWxTransferAppId(payAccountGroup.getWxTransferAppId());
						apiResp.getBody().setWxTransferAppName(wxPayConfig.getNote());
					}
				}
				if(StringTools.isNotBlank(payAccountGroup.getTransferConfig())) {
					CashWithdrawalConfig cashWithdrawalConfig = JsonTools.fromJson(payAccountGroup.getTransferConfig()
							, CashWithdrawalConfig.class);
					PayAccountGroupInfoResponse.Config config = new PayAccountGroupInfoResponse.Config();
					config.setMinMoneyStr(cashWithdrawalConfig.getMinMoneyStr());
					config.setMaxMoneyStr(cashWithdrawalConfig.getMaxMoneyStr());
					config.setDayUserMaxMoneyStr(cashWithdrawalConfig.getDayUserMaxMoneyStr());
					config.setAutoMaxMoneyStr(cashWithdrawalConfig.getAutoMaxMoneyStr());
					config.setCashWithdrawalWeeks(cashWithdrawalConfig.getCashWithdrawalWeeks().split(","));
					config.setCashWithdrawalTimes(cashWithdrawalConfig.getCashWithdrawalTimes());
					config.setServiceCharge(cashWithdrawalConfig.getServiceCharge());
					config.setMonthStartDay(cashWithdrawalConfig.getMonthStartDay());
					config.setMonthEndDay(cashWithdrawalConfig.getMonthEndDay());
					apiResp.getBody().setTransferConfig(config);
				}
				if(StringTools.isNotBlank(payAccountGroup.getMemberTransferConfig())) {
					CashWithdrawalConfig cashWithdrawalConfig = JsonTools.fromJson(payAccountGroup.getMemberTransferConfig()
							, CashWithdrawalConfig.class);
					PayAccountGroupInfoResponse.Config config = new PayAccountGroupInfoResponse.Config();
					config.setMinMoneyStr(cashWithdrawalConfig.getMinMoneyStr());
					config.setMaxMoneyStr(cashWithdrawalConfig.getMaxMoneyStr());
					config.setDayUserMaxMoneyStr(cashWithdrawalConfig.getDayUserMaxMoneyStr());
					config.setAutoMaxMoneyStr(cashWithdrawalConfig.getAutoMaxMoneyStr());
					config.setCashWithdrawalWeeks(cashWithdrawalConfig.getCashWithdrawalWeeks().split(","));
					config.setCashWithdrawalTimes(cashWithdrawalConfig.getCashWithdrawalTimes());
					config.setServiceCharge(cashWithdrawalConfig.getServiceCharge());
					config.setMonthStartDay(cashWithdrawalConfig.getMonthStartDay());
					config.setMonthEndDay(cashWithdrawalConfig.getMonthEndDay());
					apiResp.getBody().setMemberTransferConfig(config);
				}
			}
		}
		return apiResp;
	}

	
}
